﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// TMS创建外协发货计划
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OEMPlanCreateController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;


        public OEMPlanCreateController(ILogger<OEMPlanCreateRequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            _logger = loggerFactory.CreateLogger<OEMPlanCreateController>();

        }

        /// <summary>
        /// TMS创建外协发货计划
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostOEMPlanCreate([FromBody] OEMPlanCreateRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            TMSShipping_PlanCreate_2ResponseDto re = new TMSShipping_PlanCreate_2ResponseDto();
            string factory_code = strparm.factory_code;
            string factory_id = "";


            if (factory_code=="C01")
            {
                factory_id = "[\"tms_zh\"]";
            } 
            else if (factory_code == "E01")
            {
                factory_id = "[\"tms_sc\"]";
            }
            else if(factory_code == "B01")
            {
                factory_id = "[\"tms_qh\"]";
            }
            else if(factory_code == "A01")
            {
                factory_id = "[\"tms_gd\"]";
            }
            else if (factory_code == "G01")
            {
                factory_id = "[\"tms_ZN\"]";
            }
            else if (factory_code == "H01")
            {
                factory_id = "[\"tms_gz\"]";
            }
            else
            {
                strMsg = "factory_code=<"+ factory_code + "> not fine ";
            }

            Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
            Dictionary<string, string> plandic = new Dictionary<string, string>(); //计划表插入dic
            try
            {
                if (strMsg == "")
                {
                    var row = strparm.rows;
                    foreach (var item in row)
                    {

                        dic = new Dictionary<string, string>();
                        dic.Add("plan_rowid", item.plan_rowid);
                        DataTable check_ccode = MethodHelp.GetTableInfo("TMS0196", dic, con);//查询当前计划是否存在
                        if (check_ccode.Rows.Count > 0)
                        {
                            if (check_ccode.Rows[0]["is_push"].ToString().Trim()=="是")
                            {
                                strMsg = strMsg + "-计划流水<" + item.plan_rowid + ">存在下游单据，请通知物流，取消后再试！";
                            }
                            else
                            {
                                plandic = new Dictionary<string, string>();
                                plandic.Add("factory_code", factory_id);
                                plandic.Add("declare_factory", strparm.declare_factory);
                                plandic.Add("ship_date", strparm.ship_date);
                                plandic.Add("supplier_code", strparm.supplier_code);
                                plandic.Add("supplier_name", strparm.supplier_name);
                                plandic.Add("plan_code", item.plan_code);
                                plandic.Add("material_code", item.material_code);
                                plandic.Add("material_name", item.material_name);
                                plandic.Add("material_amount", item.material_amount.ToString());
                                plandic.Add("plan_rowid", item.plan_rowid);
                                plandic.Add("customer_code", item.customer_code);
                                plandic.Add("customer_name", item.customer_name);
                                string strSQL = MethodHelp.GetTranSQL("TMS0197", plandic, con);
                                if (strSQL != "")
                                {
                                    strSqlList.Add(strSQL.ToString());
                                }
                                else
                                {
                                    strMsg = "Parameter conversion failed";
                                }
                            }                         
                        }
                        else
                        {
                            plandic = new Dictionary<string, string>();
                            plandic.Add("factory_code", factory_id);
                            plandic.Add("declare_factory", strparm.declare_factory);
                            plandic.Add("ship_date", strparm.ship_date);
                            plandic.Add("supplier_code", strparm.supplier_code);
                            plandic.Add("supplier_name", strparm.supplier_name);
                            plandic.Add("plan_code", item.plan_code);
                            plandic.Add("material_code", item.material_code);
                            plandic.Add("material_name", item.material_name);
                            plandic.Add("material_amount", item.material_amount.ToString());
                            plandic.Add("plan_rowid", item.plan_rowid);
                            plandic.Add("customer_code", item.customer_code);
                            plandic.Add("customer_name", item.customer_name);
                            string strSQL = MethodHelp.GetTranSQL("TMS0195", plandic, con);
                            if (strSQL != "")
                            {
                                strSqlList.Add(strSQL.ToString());
                            }
                            else
                            {
                                strMsg = "Parameter conversion failed";
                            }


                        }
                    }
                    if (strMsg == "")
                    {
                        if (strSqlList.Count > 0)
                        {
                            bool A = DB.ExecuteNoQueryTran(strSqlList, con);
                            if (A)
                            {

                            }
                            else
                            {
                                strMsg = "Database update failed";
                            }

                        }
                        else
                        {
                            strMsg = "No insertion data present";
                        }
                    }
                }               
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }
            if (strMsg=="")
            {
                re.code = 0;
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }

            _logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);
        }

      
    }
}
